#ifndef __SINGAPOREBUSFAVORITESVIEW__
#define __SINGAPOREBUSFAVORITESVIEW__

// [[[ begin generated region: do not modify [Generated Includes]
#include <aknview.h>
// ]]] end generated region [Generated Includes]

#include "googlepositioning.h"
#include "positionrequest.h"
#include "SingaporeBusStructures.h"
#include "AsyncFileLoader.h"
#include "IteratorControl.h"
#include "ArrivalTimeQuery.h"
#include "IrisQuery.h"
#include "SmrtQuery.h"
#include "CSingaporeBusGenericView.h"
#include "CharUtils.h"
#include <SingaporeBus.RSG>
#include "CSingaporeBusGenericView.hrh"
#include "DrawUtils.h"

class CSingaporeBusFavoritesView;

struct CSingaporeBusFavoritesViewItem : public CBase
	{
	virtual void FormatItem(TDes& aBuf) const = 0;
	virtual void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected) const = 0;
	virtual TBool FilterItem(const TDesC8& aSearchText) const = 0;
	virtual TReal32 OrderItem() const = 0;
	virtual TBool ListBoxObserver(CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType) const = 0;
	virtual TBool HandleCommandL( TInt aCommand ) const = 0;
	virtual void NotifyConnected() const = 0;
	virtual void NotifyPositionChange() const = 0;
	};

struct CSingaporeBusFavoritesViewItem_Station : public CSingaporeBusFavoritesViewItem
	{
	const TStation* iStation;
	virtual TBool FilterItem(const TDesC8& aSearchText) const { return CSingaporeBusGenericHandler::FilterItem(iStation, aSearchText); }
	virtual TReal32 OrderItem() const  { return CSingaporeBusGenericHandler::OrderItem(iStation); }
	virtual TBool ListBoxObserver(CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType) const  { return CSingaporeBusGenericHandler::ListBoxObserver(iStation, aListBox, aEventType); }
	virtual TBool HandleCommandL( TInt aCommand ) const  { return CSingaporeBusGenericHandler::HandleCommandL(iStation, aCommand); }
	void NotifyPositionChange() const
		{
		
		}
	void NotifyConnected() const
		{
		
		}
	virtual void FormatItem(TDes& aBuf) const
	{
	aBuf.Format(_L("5\t%S \x2022 %S\t%S"), narrowToWideLC(iStation->iAddress2), narrowToWideLC(iStation->iAddress), narrowToWideLC(iStation->iStationNumber));
	CleanupStack::PopAndDestroy(3);

	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
		{
		TReal32 distance;
		doc->iPosition.Distance(iStation->iCoord, distance);
		aBuf.AppendFormat(_L(" \x21b3%.0fm"), (double)distance);
		}
	
	aBuf.Append('\t');

	if(doc->iOriginStation == iStation)
		aBuf.Append('3');
	else if(doc->iDestinationStation == iStation)
		aBuf.Append('4');
	else
		aBuf.Append('0');
	}
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected) const
		{
		
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

		if(iStation->iStationType == TStation::ELDNormal)
			doc->iSingaporeBusDrawItemsLD->DrawBusStopItemL(aDrawer, iStation, aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);
		else
			doc->iSingaporeBusDrawItemsSG->DrawBusStopItemL(aDrawer, iStation, aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);
		
		}
	};


struct CSingaporeBusFavoritesViewItem_Service : public CSingaporeBusFavoritesViewItem
	{
	const TService* iService;
	virtual TBool FilterItem(const TDesC8& aSearchText) const { return CSingaporeBusGenericHandler::FilterItem(iService, aSearchText); }
	virtual TReal32 OrderItem() const  { return CSingaporeBusGenericHandler::OrderItem(iService); }
	virtual TBool ListBoxObserver(CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType) const  { return CSingaporeBusGenericHandler::ListBoxObserver(iService, aListBox, aEventType); }
	virtual TBool HandleCommandL( TInt aCommand ) const  { return CSingaporeBusGenericHandler::HandleCommandL(iService, NULL, aCommand); }
	void NotifyPositionChange() const
		{
		
		}
	void NotifyConnected() const
		{
		
		}
	virtual void FormatItem(TDes& aBuf) const
	{
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	TBuf8<15> upperServiceKey = iService->iServiceKey;
	upperServiceKey.UpperCase();
	const TStation** pStation = doc->iServicesStationMap->Find(upperServiceKey);
	aBuf.Format(_L("6\t%S to %S\t"), narrowToWideLC(iService->iName), narrowToWideLC(iService->iTo));
	CleanupStack::PopAndDestroy(2);
	if(pStation)
		{
		const TStation* station = *pStation;
		TReal32 distance = 0;
		doc->iPosition.Distance(station->iCoord, distance);
		aBuf.AppendFormat(_L("%S \x21b3%.0fm"), narrowToWideLC(station->iAddress2), distance);
		CleanupStack::PopAndDestroy(1);
		}
	else
		{
		aBuf.AppendFormat(_L("from %S"), narrowToWideLC(iService->iFrom));		
		CleanupStack::PopAndDestroy(1);
		}
	aBuf.Append(_L("\t"));
	}
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected) const
		{
		

		}
	};

struct CSingaporeBusFavoritesViewItem_Service2 : public CSingaporeBusFavoritesViewItem
	{
	LManagedPtr<CArrivalTimeItem> iArrivalTimeItem;
	virtual TBool FilterItem(const TDesC8& aSearchText) const { return CSingaporeBusGenericHandler::FilterItem(iArrivalTimeItem.Get(), aSearchText); }
	virtual TReal32 OrderItem() const  { return CSingaporeBusGenericHandler::OrderItem(iArrivalTimeItem.Get()); }
	virtual TBool ListBoxObserver(CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType) const  { return CSingaporeBusGenericHandler::ListBoxObserver(iArrivalTimeItem.Get(), aListBox, aEventType); }
	virtual TBool HandleCommandL( TInt aCommand ) const
		{
		switch(aCommand)
			{
			case ESingaporeBusMarkFavorite:
				{
				// favoriting a service makes the service to be favorite, not the service+busstop
				CSingaporeBusGenericHandler::HandleCommandL(iArrivalTimeItem->iService, NULL, aCommand);
				return ETrue;
				}
			default:
				return CSingaporeBusGenericHandler::HandleCommandL(iArrivalTimeItem.Get(), aCommand);
			}
		}
	void NotifyPositionChange() const
		{
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		TBuf8<15> upperServiceKey = iArrivalTimeItem->iService->iServiceKey;
		upperServiceKey.UpperCase();
		const TStation** pStation = doc->iServicesStationMap->Find(upperServiceKey);
		if(pStation)
			iArrivalTimeItem->iStation = *pStation;
		}
	void NotifyConnected() const
		{
		if(iArrivalTimeItem->iRequestStatus == CArrivalTimeQuery::ERequestError)
			iArrivalTimeItem->iArrivalTimeQuery->SubmitL();
		}
	virtual void FormatItem(TDes& aBuf) const
	{
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		aBuf.Format(_L("6\t%S to %S @ %S"),
				narrowToWideLC(iArrivalTimeItem->iService->iName), narrowToWideLC(iArrivalTimeItem->iService->iTo),
				narrowToWideLC(iArrivalTimeItem->iStation->iAddress2));
		CleanupStack::PopAndDestroy(3);
		if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
			{
			TReal32 distance = 0;
			doc->iPosition.Distance(iArrivalTimeItem->iStation->iCoord, distance);
			aBuf.AppendFormat(_L("(%.0fm)"), distance);
			}
		aBuf.AppendFormat(_L("\t"));
		switch(iArrivalTimeItem->iRequestStatus)
			{
			case CArrivalTimeQuery::ERequestWait:
				aBuf.AppendFormat(_L("select to request"));
				break;
			case CArrivalTimeQuery::ERequestPending:
				aBuf.AppendFormat(_L("requesting..."));
				break;
			case CArrivalTimeQuery::ERequestPendingInitialization:
				aBuf.AppendFormat(_L("waiting initialisation..."));
				break;
			case CArrivalTimeQuery::ERequestCompleted:
				aBuf.AppendFormat(_L("%S \xf814 %S"), iArrivalTimeItem->iNextBus, iArrivalTimeItem->iSubsequentBus);
				break;
			case CArrivalTimeQuery::ERequestUnavailable:
				aBuf.AppendFormat(_L("arrival time unavailable"));
				break;
			case CArrivalTimeQuery::ERequestError:
				aBuf.AppendFormat(_L("error (%S, %S)"), iArrivalTimeItem->iNextBus, iArrivalTimeItem->iSubsequentBus);
				break;
			}
		aBuf.AppendFormat(_L("\t0"));
	}
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected) const
		{

		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

		if(iArrivalTimeItem->iService->iOperator == _L8("TFL"))
			doc->iSingaporeBusDrawItemsLD->DrawBusServiceItemL(aDrawer, iArrivalTimeItem.Get(), aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);
		else
			doc->iSingaporeBusDrawItemsSG->DrawBusServiceItemL(aDrawer, iArrivalTimeItem.Get(), aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);

		}
	};


struct CSingaporeBusFavoritesViewItem_ArrivalTime : public CSingaporeBusFavoritesViewItem
	{
	LManagedPtr<CArrivalTimeItem> iArrivalTimeItem;
	virtual TBool FilterItem(const TDesC8& aSearchText) const { return CSingaporeBusGenericHandler::FilterItem(iArrivalTimeItem.Get(), aSearchText); }
	virtual TReal32 OrderItem() const  { return CSingaporeBusGenericHandler::OrderItem(iArrivalTimeItem.Get()); }
	virtual TBool ListBoxObserver(CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType) const  { return CSingaporeBusGenericHandler::ListBoxObserver(iArrivalTimeItem.Get(), aListBox, aEventType); }
	virtual TBool HandleCommandL( TInt aCommand ) const  { return CSingaporeBusGenericHandler::HandleCommandL(iArrivalTimeItem.Get(), aCommand); }
	void NotifyPositionChange() const
		{
		
		}
	void NotifyConnected() const
		{
		if(iArrivalTimeItem->iRequestStatus == CArrivalTimeQuery::ERequestError)
			iArrivalTimeItem->iArrivalTimeQuery->SubmitL();
		}
	virtual void FormatItem(TDes& aBuf) const
	{
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	aBuf.Format(_L("7\t%S to %S @ %S"),
			narrowToWideLC(iArrivalTimeItem->iService->iName), narrowToWideLC(iArrivalTimeItem->iService->iTo),
			narrowToWideLC(iArrivalTimeItem->iStation->iAddress2));
	CleanupStack::PopAndDestroy(3);
	if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
		{
		TReal32 distance = 0;
		doc->iPosition.Distance(iArrivalTimeItem->iStation->iCoord, distance);
		aBuf.AppendFormat(_L("(%.0fm)"), distance);
		}
	aBuf.AppendFormat(_L("\t"));
	switch(iArrivalTimeItem->iRequestStatus)
		{
		case CArrivalTimeQuery::ERequestWait:
			aBuf.AppendFormat(_L("select to request"));
			break;
		case CArrivalTimeQuery::ERequestPending:
			aBuf.AppendFormat(_L("requesting..."));
			break;
		case CArrivalTimeQuery::ERequestPendingInitialization:
			aBuf.AppendFormat(_L("waiting initialisation..."));
			break;
		case CArrivalTimeQuery::ERequestCompleted:
			aBuf.AppendFormat(_L("%S \xf814 %S"), iArrivalTimeItem->iNextBus, iArrivalTimeItem->iSubsequentBus);
			break;
		case CArrivalTimeQuery::ERequestUnavailable:
			aBuf.AppendFormat(_L("arrival time unavailable"));
			break;
		case CArrivalTimeQuery::ERequestError:
			aBuf.AppendFormat(_L("error (%S, %S)"), iArrivalTimeItem->iNextBus, iArrivalTimeItem->iSubsequentBus);
			break;
		}
	aBuf.AppendFormat(_L("\t0"));
	}
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected) const
		{

		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

		if(iArrivalTimeItem->iStation->iStationType == TStation::ELDNormal)
			doc->iSingaporeBusDrawItemsLD->DrawBusServiceArrivalTimeWithStationItemL(aDrawer, iArrivalTimeItem.Get(), aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);
		else
			doc->iSingaporeBusDrawItemsSG->DrawBusServiceArrivalTimeWithStationItemL(aDrawer, iArrivalTimeItem.Get(), aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);

		}
	};


class CSingaporeBusFavoritesView  : public CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>
	{
	
public:
	
	
	RArray<const CSingaporeBusFavoritesViewItem*> iItems; 
	
	virtual CEikListBox* CreateListBoxL()
		{
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		const TInt minHeight = doc->iSingaporeBusDrawItemsSG->PrimaryFont()->FontMaxHeight() + doc->iSingaporeBusDrawItemsSG->SecondaryFont()->FontMaxHeight()*2;
		return ConstructListBoxL(
				new (ELeave) CSingaporeBusListBox,
				minHeight + (ClientRect().Height() % minHeight)/(ClientRect().Height() / minHeight)
				);
		//return ConstructListBoxL(new (ELeave) CAknDoubleGraphicStyleListBox);
		}
	void FormatItem(TDes& aBuf, const CSingaporeBusFavoritesViewItem* aItem) { return aItem->FormatItem(aBuf); }
	
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer, const CSingaporeBusFavoritesViewItem* aItem,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected)
		{
		aItem->DrawItem(aDrawer,
				aItemIndex, aActualItemRect,
				aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, 
				aItemIsSelected);
		}
	
	TBool FilterItem(const CSingaporeBusFavoritesViewItem* aItem, const TDesC8& aFilter) { return aItem->FilterItem(aFilter); }
	TReal32 OrderItem(const CSingaporeBusFavoritesViewItem* aItem) { return aItem->OrderItem(); }
	TBool ListBoxObserver(const CSingaporeBusFavoritesViewItem* aItem, CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType)  { return aItem->ListBoxObserver(aListBox, aEventType); }
	TBool HandleCommandL(const CSingaporeBusFavoritesViewItem* aItem, TInt aCommand ) 
		{
		this->iContainer->iIteratorControl->DrawDeferred();
		switch(aCommand)
			{
			case ESingaporeBusRefreshAll:
				{
				for(TInt i = 0 ; i < iItems.Count() ; ++i)
					iItems[i]->HandleCommandL(aCommand);
				return ETrue;
				}
			default:
				return aItem->HandleCommandL(aCommand);
			}
		}
	
    TInt iCurrentFavorite;
    
	void NotifyConnected()
		{
		TInt countItems = iItems.Count();
		for(TInt i = 0 ; i < iItems.Count() ; ++i)
			{
			iItems[i]->NotifyConnected();
			}
		
		}
 
	void NotifyPositionChange()
		{
		for(TInt i = 0 ; i < iItems.Count() ; ++i)
			{
			iItems[i]->NotifyPositionChange();
			}
		CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>::NotifyPositionChange();
		}

	
	void ConstructL()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>::ConstructL( R_SINGAPORE_BUS_FAVORITES_VIEW, R_SINGAPORE_BUS_STOPS_MARK_MENU, R_SINGAPORE_BUS_STOPS_ICONS, ESingaporeBusFavoritesViewId );
		}
						
	void StoreViewStateL(TDes8& aCustomMessage)
		{
		CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>::StoreViewStateL(aCustomMessage);
		StoreCustomMessageParamL(aCustomMessage, _L8("id"), _L8("%d"), iCurrentFavorite);
		}
	
	~CSingaporeBusFavoritesView()
		{
		iItems.Close();
		}
	
	
protected:
	void DoActivateL(
		const TVwsViewId& aPrevViewId,
		TUid aCustomMessageId,
		const TDesC8& aCustomMessage )
		{
		CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>::DoActivateL(aPrevViewId, aCustomMessageId, aCustomMessage );
		iCurrentFavorite = 0;
		
		TPtrC8 field, value, remainder(aCustomMessage);
		while(ParseCustomMessageParamL(field, value, remainder))
			{
			if(field == _L8("id"))
				{
				TLex8 lex(value);
				lex.Val(iCurrentFavorite);
				}
			}
		InitializeList();
		RestoreViewStateL(aCustomMessage);
		
		}
	void DoDeactivate()
		{
		this->SetEmptyUnderlyingL();
		for(TInt i = 0 ; i < iItems.Count() ; ++i)
			delete iItems[i];
		iItems.Reset();
		CSingaporeBusGenericView<const CSingaporeBusFavoritesViewItem*, CSingaporeBusFavoritesView>::DoDeactivate();
		}
		
	
public:	
	void InitializeList()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);

			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			
			THashSetIter< TBuf8<15> > it (*doc->iFavorites[iCurrentFavorite]);
			this->SetEmptyUnderlyingL();
			for(TInt i = 0 ; i < iItems.Count() ; ++i)
				delete iItems[i];
			iItems.Reset();
			for(it.Next() ; it.Current() ; it.Next())
				{
				const TDesC8& strFavorite = *it.Current();
				
				if(strFavorite[0] == '&')
					{
					LCleanedupPtr<CSingaporeBusFavoritesViewItem_ArrivalTime> newItem ( new (ELeave) CSingaporeBusFavoritesViewItem_ArrivalTime );
					TInt sep = strFavorite.Locate(' ');
					TPtrC8 strStation( strFavorite.Mid(1, sep-1) );
					TPtrC8 strServiceKey( strFavorite.Mid(sep+1) );
					
					const TService* service = NULL;
					{
					TBuf8<15> strServiceKeyUpper(strServiceKey);
					strServiceKeyUpper.UpperCase();
					TService target = { strServiceKeyUpper };
					TInt ret = doc->iServicesSortedByServiceKey.SpecificFindInOrder(&target, TLinearOrder<const TService*>(&CSingaporeBusDocument::TLinearOrder_Service), EArrayFindMode_Any);
					service = (ret != KErrNotFound) ? doc->iServicesSortedByServiceKey[ret] : NULL;
					}
					
					const TStation* station = NULL;
					{
					TStation target = { strStation };
					TInt ret = doc->iStationsSortedByStationNumber.SpecificFindInOrder(&target, TLinearOrder<const TStation*>(&CSingaporeBusDocument::TLinearOrder_Station), EArrayFindMode_Any);
					station = (ret != KErrNotFound) ? doc->iStationsSortedByStationNumber[ret] : NULL;
					}

					if(service && station)
						{
						newItem->iArrivalTimeItem = CSingaporeBusGenericHandler::NewArrivalTimeItemL(station, service, this);
						iItems.AppendL(newItem.Unmanage());
						}
					
					}
				else if(strFavorite[0] == '#')
					{
					LCleanedupPtr<CSingaporeBusFavoritesViewItem_Service2> newItem ( new (ELeave) CSingaporeBusFavoritesViewItem_Service2 );
					TBuf8<15> serviceKeyUpper(strFavorite.Mid(1));
					serviceKeyUpper.UpperCase();
					const TService* service = NULL;
					{
					TService target = { serviceKeyUpper };
					TInt ret = doc->iServicesSortedByServiceKey.SpecificFindInOrder(&target, TLinearOrder<const TService*>(&CSingaporeBusDocument::TLinearOrder_Service), EArrayFindMode_Any);
					service = (ret != KErrNotFound) ? doc->iServicesSortedByServiceKey[ret] : NULL;
					}
					const TStation* station = NULL;
					{
					const TStation** pStation = doc->iServicesStationMap->Find(serviceKeyUpper);
					if(pStation)
						station = *pStation;
					else
						{
						TStation target = { doc->iServiceStations[service->iStationsBeg] };
						TInt ret = doc->iStationsSortedByStationNumber.SpecificFindInOrder(&target, TLinearOrder<const TStation*>(&CSingaporeBusDocument::TLinearOrder_Station), EArrayFindMode_Any);
						station = (ret != KErrNotFound) ? doc->iStationsSortedByStationNumber[ret] : NULL;
						}
					}
					if(service && station)
						{
						newItem->iArrivalTimeItem = CSingaporeBusGenericHandler::NewArrivalTimeItemL(station, service, this);
						iItems.AppendL(newItem.Unmanage());
						}
					}
				else if(strFavorite[0] == '#')
					{
					TBuf8<15> serviceKeyUpper(strFavorite.Mid(1));
					serviceKeyUpper.UpperCase();
					TService target = { serviceKeyUpper };
					LCleanedupPtr<CSingaporeBusFavoritesViewItem_Service> newItem ( new (ELeave) CSingaporeBusFavoritesViewItem_Service );
					TInt ret = doc->iServicesSortedByServiceKey.SpecificFindInOrder(&target, TLinearOrder<const TService*>(&CSingaporeBusDocument::TLinearOrder_Service), EArrayFindMode_Any);
					newItem->iService = (ret != KErrNotFound) ? doc->iServicesSortedByServiceKey[ret] : NULL;
					iItems.AppendL(newItem.Unmanage());
					}
				else
					{
					TStation target = { strFavorite };
					LCleanedupPtr<CSingaporeBusFavoritesViewItem_Station> newItem ( new (ELeave) CSingaporeBusFavoritesViewItem_Station );
					TInt ret = doc->iStationsSortedByStationNumber.SpecificFindInOrder(&target, TLinearOrder<const TStation*>(&CSingaporeBusDocument::TLinearOrder_Station), EArrayFindMode_Any);
					newItem->iStation = (ret != KErrNotFound) ? doc->iStationsSortedByStationNumber[ret] : NULL;
					iItems.AppendL(newItem.Unmanage());
					}
					
				
				}
			if(iItems.Count() > 0)
				this->SetUnderlyingL(
						new (ELeave) CArrayIterator<const CSingaporeBusFavoritesViewItem*>(&iItems[0], iItems.Count())
						);
			else
				{
				this->SetEmptyUnderlyingL();
				iContainer->iIteratorControl->iListBox->View()->SetListEmptyTextL(_L("No favorites selected"));
				iContainer->iIteratorControl->iListBox->DrawDeferred();
				}
		}    
	
		
	};

#endif // __SINGAPOREBUSFAVORITESVIEW__
